//
//  ActivityViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/11/2.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "ActivityViewController.h"
#import "CLUIBaseTableViewController+Empty.h"
#import "ActivityDetailViewController.h"

@interface ActivityViewController ()
@property (strong, nonatomic) NSMutableArray *arrDataSource;
@property (assign, nonatomic) NSInteger page;
@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"活动";//
    [self showNavgationWithLeftItemWithTitle:@"首页"];
    self.page = 1;
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        if (_arrDataSource.count == 0) {
            weakSelf.page = 1;
        }else{
            weakSelf.page ++;
        }
        [weakSelf getActivityList];
    }];
    [self getActivityList];
}

- (void)getActivityList{
    [SVProgressHUD showWithStatus:@"获取活动列表.." maskType:SVProgressHUDMaskTypeClear];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[NSNumber numberWithInteger:self.page] forKey:@"page"];
    WS(weakSelf);
    [[BaseDataRequest alloc] requestActivitList:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        [self.tableView.footer endRefreshing];
        if ([status isEqualToString:@"0"]) {
            NSMutableArray *arrResult = [NSMutableArray arrayWithArray:[responseObject objectForKey:@"result"]];
            if (arrResult.count == 0 || arrResult.count < 10) {
                [self.tableView.footer noticeNoMoreData];
            }
            [self.arrDataSource addObjectsFromArray:arrResult];
            [self.tableView reloadData];
        }else{
            if (self.tableView.footer.isRefreshing) {
                if (self.page>1) {
                    self.page--;
                }
            }
            if(self.arrDataSource.count == 0){
                [weakSelf showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"出错了，稍后再试吧" withImage:[UIImage imageNamed:@"icon_wifi"]];
                self.tableView.footer.hidden = YES;
            }else{
                if (error != nil) {
                    TIPSFORVIEW(@"请检测网络设置");
                }else{
                    TIPSFORVIEW([responseObject objectForKey:@"message"]);
                }
            }
        }
    }];
}

#pragma  mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"ActivityViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    UIImageView *imageV = [cell viewWithTag:1];
    UILabel *labTitle = [cell viewWithTag:2];
    UILabel *labAddress = [cell viewWithTag:3];
    UILabel *labTime = [cell viewWithTag:4];
    NSMutableDictionary *dicr = _arrDataSource[indexPath.row];
    [imageV sd_setImageWithURL:[NSURL URLWithString:[dicr objectForKey:@"act_image"]] placeholderImage:[UIImage imageNamed:@"iocn_default"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if(image == nil){
            imageV.image = [UIImage imageNamed:@"icon_default"];
        }
    }];
    labTitle.text= [dicr objectForKey:@"act_title"];
    labAddress.text = [NSString stringWithFormat:@"活动地点：%@",[dicr objectForKey:@"act_detail_address"]];
    labTime.text = [NSString stringWithFormat:@"活动时间：%@",[dicr objectForKey:@"act_start_time"]];
    return cell;
}

#pragma mark -  UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary *dicr = _arrDataSource[indexPath.row];
    ActivityDetailViewController *activityDetail = [[UIStoryboard storyboardWithName:@"ActivityViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"ActivityDetailViewController"];
    activityDetail.strActivityURL = [dicr objectForKey:@"act_detail_url"];
    [self.navigationController pushViewController:activityDetail animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (SCREEN_WIDTH > 320) {
        return (0.5)*SCREEN_WIDTH+100;
    }else{
        return 259;
    }
}

#pragma mark - set/get
- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
